import apis from "../services/messageApi";

export default {
  namespace: "message",
  state: {
    messageList: [],
    totalCount: 0,
    loading: false,
    errExport: -1,
    errMsg: "",
    excelData: [],
    departData: []
  },
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     // ...
  //   }
  // },
  effects: {
    // 推送数量趋势图
    *messageList({ payload }, { call, put }) {
      yield put({
        type: "put/putloading",
        payload: true
      });
      const res = yield call(apis.messageList, payload);
      const { datas, totalCount } = res;
      yield put({
        type: "get/getmessageList",
        payload: datas
      });
      yield put({
        type: "put/putloading",
        payload: false
      });
      yield put({
        type: "put/puttotal",
        payload: totalCount
      });
    },
    *exportExcel({ payload }, { call, put }) {
      const res = yield call(apis.exportExcel, payload);
      const { datas, errCode, errMsg } = res;
      yield put({
        type: "get/excel",
        payload: datas
      });
      yield put({
        type: "get/errExport",
        payload: errCode
      });
      yield put({
        type: "get/errMsg",
        payload: errMsg
      });
    },
    *getDepart({ payload }, { call, put }) {
      const res = yield call(apis.getDepart, payload);
      const { datas = [] } = res;
      yield put({
        type: "get/depart",
        payload: datas
      });
    }
  },
  reducers: {
    "get/getmessageList": (state, { payload }) => ({
      ...state,
      messageList: payload
    }),
    "put/putloading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/puttotal": (state, { payload }) => ({
      ...state,
      totalCount: payload
    }),
    "get/errExport": (state, { payload }) => ({
      ...state,
      errExport: payload
    }),
    "get/errMsg": (state, { payload }) => ({
      ...state,
      errMsg: payload
    }),
    "get/excel": (state, { payload }) => ({
      ...state,
      excelData: payload
    }),
    "get/depart": (state, { payload }) => ({
      ...state,
      departData: payload
    })
  }
};
